import { ref } from 'vue'
import {loginAPI} from "@/api/user.js";
import router from "@/router/index.js";
import { defineStore } from "pinia";

export const useUserStore = defineStore('user', () => {
  const token = ref('')
  const userInfo = ref({})

  const login = async (data) => {
    const res = await loginAPI(data)

    token.value = res.data.token;
    userInfo.value = res.data
  }

  const logout = () => {
    token.value = ''
    userInfo.value = {}

    router.replace('/login')
  }

  return {
    token,
    userInfo,
    login,
    logout
  }
}, {
  persist: true
})
